/***
 * Excerpted from "Hello, Android! 3e",
 * published by The Pragmatic Bookshelf.
 * Copyrights apply to this code. It may not be used to create training material, 
 * courses, books, articles, and the like. Contact us if you are in doubt.
 * We make no guarantees that this code is fit for any purpose. 
 * Visit http://www.pragmaticprogrammer.com/titles/eband3 for more book information.
 ***/
package org.example.sudoku;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;

public class Sudoku extends Activity implements OnClickListener {
	private static final String TAG = "Sudoku";

	 Intent timer;
	
	// public Broker b ;
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {// pkrenuta aplk
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);// iscrtava meni

		// Set up click listeners for all the buttons
		View continueButton = findViewById(R.id.continue_button);// "pravis"
		continueButton.setOnClickListener(this); // sta da radi na click
		View newButton = findViewById(R.id.new_button);
		newButton.setOnClickListener(this);
		View aboutButton = findViewById(R.id.about_button);
		aboutButton.setOnClickListener(this);
		View exitButton = findViewById(R.id.exit_button);
		exitButton.setOnClickListener(this);

		//View todoButton = findViewById(R.id.todo_button);
		//todoButton.setOnClickListener(this);

		// izbrisati; podaci iz baze treba da se citaju pri izboru igre
		// View bazaButton = findViewById(R.id.baza_button);
		// bazaButton.setOnClickListener(this);

	}

	@Override
	protected void onResume() {
		super.onResume();
		// Music.play(this, R.raw.main);
	}

	@Override
	protected void onPause() {
		super.onPause();
		// Music.stop(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.continue_button:
			startGame(Game.DIFFICULTY_CONTINUE);
			break;
		// ...
		case R.id.about_button:

			
			Intent i = new Intent(this, About.class);
			startActivity(i);
			break;
		// More buttons go here (if any) ...
		case R.id.new_button:
			//timer.startujTimer();//start service
			timer=new Intent(this, TimerSudoku.class);
			startService(timer);
			openNewGameDialog();
			break;
		case R.id.exit_button:
			
			//timer.stopirajTimer();//stop service
			stopService(timer);
			finish();
			break;

		case R.id.todo_button:// baza
			prikazi_podatke_iz_baze();
			// startActivity(new Intent(this,TodosOverviewActivity.class));
			break;
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.settings:
			startActivity(new Intent(this, Prefs.class));
			return true;
			// More items go here (if any) ...
		}
		return false;
	}

	/** Ask the user what difficulty level they want */

	private void openNewGameDialog() {
		new AlertDialog.Builder(this)
				.setTitle(R.string.new_game_title)
				.setItems(R.array.difficulty,
						new DialogInterface.OnClickListener() {
							public void onClick(
									DialogInterface dialoginterface, int i) {
								startGame(i);
							}
						}).show();
	}

	/** Start a new game with the given difficulty level */
	private void startGame(int i) {
		Log.d(TAG, "clicked on " + i);
		Intent intent = new Intent(this, Game.class);
		intent.putExtra(Game.KEY_DIFFICULTY, i);

		
		
		startActivity(intent);

	}

	

	private void prikazi_podatke_iz_baze() {

		/*
		 * b = new Broker(this); String a="nije ucitano";
		 * 
		 * try { //b.onCreate(b.getWritableDatabase()); //treba da se proveri da
		 * li je kreirana baza //b.getWritableDatabase();
		 * b.insert("alio string", "1"); Cursor cursor = b.izvrsiUpit();
		 * 
		 * a= b.prikaziPodatke(cursor); } finally { b.close(); }
		 */

		/*
		 * new AlertDialog.Builder(this) .setTitle("podaci iz baze")
		 * .setMessage(a) .show();
		 */
	}

}